package com.moyou.weichat.dapter;

import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.util.DateUtils;
import com.hyphenate.util.EMLog;
import com.moyou.weichat.R;
import com.moyou.weichat.bean.ChatTypeBean;
import com.moyou.weichat.chat.ChatActivity;
import com.moyou.weichat.utils.ChatUtils;
import com.moyou.weichat.utils.ConstantUtils;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/10/18 0018.
 * sj: 2017年11月9日11:16:07
 * name :ts
 * 用途：聊天适配器逻辑操作
 */

public abstract class BaseChatAdapter extends BaseMultiItemQuickAdapter<ChatTypeBean, BaseViewHolder> {
    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     * @param data A new list is created out of this one to avoid mutable list
     */
    private static final int HANDLER_MESSAGE_REFRESH_LIST = 0;
    private static final int HANDLER_MESSAGE_SELECT_LAST = 1;
    private static final int HANDLER_MESSAGE_SEEK_TO = 2;

    private int chatType;
    private String userId;
    private RecyclerView chatTypeBeenList;
    private EMConversation conversation;
    private List<EMMessage> messages = null;


    Handler handler = new Handler() {
        private void refreshList() {
            // you should not call getAllMessages() in UI thread
            // otherwise there is problem when refreshing UI and there is new message arrive
            messages = conversation.getAllMessages();
            setNewData(ChatUtils.getChatUtils(mContext).getMessage(messages, conversation));
            conversation.markAllMessagesAsRead();
            notifyDataSetChanged();
        }

        @Override
        public void handleMessage(android.os.Message message) {
            switch (message.what) {
                case HANDLER_MESSAGE_REFRESH_LIST:
                    refreshList();
                    break;
                case HANDLER_MESSAGE_SELECT_LAST:
                    if (messages != null && messages.size() > 0) {
                        chatTypeBeenList.scrollToPosition(getItemCount() - 1);
                    }
                    break;
                case HANDLER_MESSAGE_SEEK_TO:
                    int position = message.arg1;
                    chatTypeBeenList.scrollToPosition(position);
                    break;
                default:
                    break;
            }
        }
    };

    public BaseChatAdapter(List<ChatTypeBean> data, int chatType, String userId, RecyclerView chatTypeBeenList) {
        super(data);
        this.chatType = chatType;
        this.userId = userId;
        this.chatTypeBeenList = chatTypeBeenList;
        this.conversation = EMClient.getInstance().chatManager().getConversation(userId, ChatUtils.getChatUtils(mContext).getConversationType(chatType), true);
        //左边的类型
        addItemType(ConstantUtils.CHAT_TYPE_LEFT_TEXT, R.layout.chat_text_item_left); //文本类型
        addItemType(ConstantUtils.CHAT_TYPE_LEFT_IMAGE, R.layout.chat_iamge_left_item);//图片类型
        addItemType(ConstantUtils.CHAT_TYPE_LEFT_VIDEO, R.layout.chat_video_left_item);//视频类型
        addItemType(ConstantUtils.CHAT_TYPE_LEFT_LOCATION, R.layout.chat_location_left_item);//定位类型
        addItemType(ConstantUtils.CHAT_TYPE_LEFT_VOICE, R.layout.chat_voice_item_left);//语音类型
        //右边的类型
        addItemType(ConstantUtils.CHAT_TYPE_RIGHT_TEXT, R.layout.chat_text_item_rigth);//文本类型
        addItemType(ConstantUtils.CHAT_TYPE_RIGTH_IMAGE, R.layout.chat_iamge_right_item);//图片类型
        addItemType(ConstantUtils.CHAT_TYPE_RIGTH_VIDEO, R.layout.chat_video_right_item);//视频类型
        addItemType(ConstantUtils.CHAT_TYPE_RIGTH_LOCATION, R.layout.chat_location_right_item);//定位类型
        addItemType(ConstantUtils.CHAT_TYPE_RIGTH_VOICE, R.layout.chat_voice_item_rigth);//图片类型
    }

    @Override
    protected void convert(BaseViewHolder helper, ChatTypeBean item) {
        //单聊
        if (chatType == ConstantUtils.CHAT_TYPE_SING) {
            if (helper.getItemViewType() == ConstantUtils.CHAT_TYPE_LEFT_TEXT) {
                //左边文本
                chatSignLeftSetTextUI(helper,item);
            } else if (helper.getItemViewType() == ConstantUtils.CHAT_TYPE_LEFT_IMAGE) {
                //左边图片
                chatSignLeftSetImageUI(helper,item);
            }else if (helper.getItemViewType() == ConstantUtils.CHAT_TYPE_LEFT_VIDEO){
                //左边视频
                chatSignLeftSetVideoUI(helper,item);
            }else if (helper.getItemViewType() == ConstantUtils.CHAT_TYPE_LEFT_LOCATION){
                //左边定位
                chatSignLeftSetLocationUI(helper,item);
            }else if (helper.getItemViewType() == ConstantUtils.CHAT_TYPE_LEFT_VOICE){
                //左边音频
                chatSignLeftSetVioceUI(helper,item);
            } else if (helper.getItemViewType() == ConstantUtils.CHAT_TYPE_RIGHT_TEXT) {
                //右边文本
                chatSignRightSetTextUI(helper , item);
            }else if (helper.getItemViewType() == ConstantUtils.CHAT_TYPE_RIGTH_IMAGE ){
                //右边图片
                chatSignRightSetImageUI(helper,item);
            }else if (helper.getItemViewType() == ConstantUtils.CHAT_TYPE_RIGTH_VIDEO){
                //右边视频
                chatSignRightSetVideoUI(helper,item);
            }else if (helper.getItemViewType() == ConstantUtils.CHAT_TYPE_RIGTH_LOCATION){
                //右边定位
                chatSignRightSetLocationUI(helper,item);
            }else if (helper.getItemViewType() == ConstantUtils.CHAT_TYPE_RIGTH_VOICE){
                //右边音频
                chatSignRightSetVioceUI(helper,item);
            }
            //设置单聊相同的UI
            chatSignAllUI(helper,item);
        }
        //群聊
        else if (chatType == ConstantUtils.CHAT_TYPE_GROUP) {
            if (helper.getItemViewType() == ConstantUtils.CHAT_TYPE_LEFT_TEXT) {
                //左边文本
                chatGroupLeftSetTextUI(helper,item);
            } else if (helper.getItemViewType() == ConstantUtils.CHAT_TYPE_LEFT_IMAGE) {
                //左边图片
                chatGroupLeftSetImageUI(helper,item);
            }else if (helper.getItemViewType() == ConstantUtils.CHAT_TYPE_LEFT_VIDEO){
                //左边视频
                chatGroupLeftSetVideoUI(helper,item);
            }else if (helper.getItemViewType() == ConstantUtils.CHAT_TYPE_LEFT_LOCATION){
                //左边定位
                chatGroupLeftSetLocationUI(helper,item);
            }else if (helper.getItemViewType() == ConstantUtils.CHAT_TYPE_LEFT_VOICE){
                //左边音频
                chatGroupLeftSetVioceUI(helper,item);
            } else if (helper.getItemViewType() == ConstantUtils.CHAT_TYPE_RIGHT_TEXT) {
                //右边文本
                chatGroupRightSetTextUI(helper,item);
            }else if (helper.getItemViewType() == ConstantUtils.CHAT_TYPE_RIGTH_IMAGE){
                //右边图片
                chatGroupRightSetImageUI(helper,item);
            }else if (helper.getItemViewType() == ConstantUtils.CHAT_TYPE_RIGTH_VIDEO){
                //右边视频
                chatGroupRightSetVideoUI(helper,item);
            }else if (helper.getItemViewType() == ConstantUtils.CHAT_TYPE_RIGTH_LOCATION){
                //右边定位
                chatGroupRightSetLocationUI(helper,item);
            }else if (helper.getItemViewType() == ConstantUtils.CHAT_TYPE_RIGTH_VOICE){
                //右边音频
                chatGroupRightSetVioceUI(helper,item);
            }
            //设置群聊相同的UI
            chatGroupAllUI(helper,item);
        }
    }

    //设置单聊左边的UI
    public abstract void chatSignLeftSetTextUI(BaseViewHolder helper, ChatTypeBean item);

    //设置单聊右边边的UI
    public abstract void chatSignRightSetTextUI(BaseViewHolder helper, ChatTypeBean item);

    //设置单聊左边的UI
    public abstract void chatSignLeftSetImageUI(BaseViewHolder helper, ChatTypeBean item);

    //设置单聊右边边的UI
    public abstract void chatSignRightSetImageUI(BaseViewHolder helper, ChatTypeBean item);
    //设置单聊左边的UI
    public abstract void chatSignLeftSetVideoUI(BaseViewHolder helper, ChatTypeBean item);

    //设置单聊右边边的UI
    public abstract void chatSignRightSetVideoUI(BaseViewHolder helper, ChatTypeBean item);

    //设置单聊左边的UI
    public abstract void chatSignLeftSetVioceUI(BaseViewHolder helper, ChatTypeBean item);

    //设置单聊右边边的UI
    public abstract void chatSignRightSetVioceUI(BaseViewHolder helper, ChatTypeBean item);
    //设置单聊左边的UI
    public abstract void chatSignLeftSetLocationUI(BaseViewHolder helper, ChatTypeBean item);

    //设置单聊右边边的UI
    public abstract void chatSignRightSetLocationUI(BaseViewHolder helper, ChatTypeBean item);

    //设置单聊共同UI
    public abstract void chatSignAllUI(BaseViewHolder helper, ChatTypeBean item);

    // -----------------------------群聊

    //设置单聊左边的UI
    public abstract void chatGroupLeftSetTextUI(BaseViewHolder helper, ChatTypeBean item) ;

    //设置单聊右边边的UI
    public abstract void chatGroupRightSetTextUI(BaseViewHolder helper, ChatTypeBean item) ;

    //设置单聊左边的UI
    public abstract void chatGroupLeftSetImageUI(BaseViewHolder helper, ChatTypeBean item) ;

    //设置单聊右边边的UI
    public abstract void chatGroupRightSetImageUI(BaseViewHolder helper, ChatTypeBean item) ;
    //设置单聊左边的UI
    public abstract void chatGroupLeftSetVideoUI(BaseViewHolder helper, ChatTypeBean item) ;

    //设置单聊右边边的UI
    public abstract void chatGroupRightSetVideoUI(BaseViewHolder helper, ChatTypeBean item) ;

    //设置单聊左边的UI
    public abstract void chatGroupLeftSetVioceUI(BaseViewHolder helper, ChatTypeBean item) ;

    //设置单聊右边边的UI
    public abstract void chatGroupRightSetVioceUI(BaseViewHolder helper, ChatTypeBean item) ;
    //设置单聊左边的UI
    public abstract void chatGroupLeftSetLocationUI(BaseViewHolder helper, ChatTypeBean item) ;

    //设置单聊右边边的UI
    public abstract void chatGroupRightSetLocationUI(BaseViewHolder helper, ChatTypeBean item) ;

    //设置群聊共同UI
    public abstract void chatGroupAllUI(BaseViewHolder helper, ChatTypeBean item) ;


    //通知聊天列表去刷新
    public void refreshSelectLast(ChatTypeBean chatTypeBea, List<ChatTypeBean> chatTypeBeenList) {
        chatTypeBeenList.add(chatTypeBea);
        addData(chatTypeBeenList.size(), chatTypeBea);
        notifyItemChanged(chatTypeBeenList.size());
    }

    /**
     * 刷新List然后在滑动到最后一个
     */
    public void refreshSelectLast() {
        final int TIME_DELAY_REFRESH_SELECT_LAST = 100;
        handler.removeMessages(HANDLER_MESSAGE_REFRESH_LIST);
        handler.removeMessages(HANDLER_MESSAGE_SELECT_LAST);
        handler.sendEmptyMessageDelayed(HANDLER_MESSAGE_REFRESH_LIST, TIME_DELAY_REFRESH_SELECT_LAST);
        handler.sendEmptyMessageDelayed(HANDLER_MESSAGE_SELECT_LAST, TIME_DELAY_REFRESH_SELECT_LAST);
    }

    /**
     * refresh and seek to the position
     */
    public void refreshSeekTo(int position) {
        handler.sendMessage(handler.obtainMessage(HANDLER_MESSAGE_REFRESH_LIST));
    }

    public void refresh() {
        if (handler.hasMessages(HANDLER_MESSAGE_REFRESH_LIST)) {
            return;
        }
        android.os.Message msg = handler.obtainMessage(HANDLER_MESSAGE_REFRESH_LIST);
        handler.sendMessage(msg);
    }

    //设置显示的时间
    protected void setUpBaseView(BaseViewHolder helper,int position,ChatTypeBean chatTypeBean ) {
        // set nickname, avatar and background of bubble
        TextView timestamp =helper.getView(R.id.chat_timer);
        if (timestamp != null) {
            if (position == 0) {
                timestamp.setText(DateUtils.getTimestampString(new Date(chatTypeBean.getMsgTime())));
                timestamp.setVisibility(View.VISIBLE);
            } else {
                // show time stamp if interval with last message is > 30 seconds
                ChatTypeBean prevMessage = getItem(position - 1);
                if (chatTypeBean != null && DateUtils.isCloseEnough(chatTypeBean.getMsgTime() , prevMessage.getMsgTime())) {
                    timestamp.setVisibility(View.GONE);
                } else {
                    timestamp.setText(DateUtils.getTimestampString(new Date(chatTypeBean.getMsgTime())));
                    timestamp.setVisibility(View.VISIBLE);
                }
            }
        }
    }


}
